package com.example.netdemo.slice;

import com.example.netdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.Revocable;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.net.*;
//import ohos.net.NetStatusCallback;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainAbilitySlice extends AbilitySlice {
    HttpURLConnection connection = null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        NetManager netManager = NetManager.getInstance(getContext());
        NetHandle netHandle = netManager.getDefaultNet();
        NetStatusCallback callback = new NetStatusCallback() {
            @Override
            public void onAvailable(NetHandle handle) {
                super.onAvailable(handle);
            }

            @Override
            public void onBlockedStatusChanged(NetHandle handle, boolean blocked) {
                super.onBlockedStatusChanged(handle, blocked);
            }

            @Override
            public void onLosing(NetHandle handle, long maxMsToLive) {
                super.onLosing(handle, maxMsToLive);
            }

            @Override
            public void onLost(NetHandle handle) {
                super.onLost(handle);
            }

            @Override
            public void onUnavailable() {
                super.onUnavailable();
            }

            @Override
            public void onCapabilitiesChanged(NetHandle handle, NetCapabilities networkCapabilities) {
                super.onCapabilitiesChanged(handle, networkCapabilities);
            }

            @Override
            public void onConnectionPropertiesChanged(NetHandle handle, ConnectionProperties connectionProperties) {
                super.onConnectionPropertiesChanged(handle, connectionProperties);
            }
        };
        netManager.addDefaultNetStatusCallback(callback);

        Text text = findComponentById(ResourceTable.Id_text_helloworld);

        try {
            URL url = new URL("https://www.baidu.com");
            connection = (HttpURLConnection) netHandle.openConnection(url, java.net.Proxy.NO_PROXY);
            connection.setRequestMethod("GET");


        } catch (IOException e) {
            e.printStackTrace();
        }
        TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
        Revocable revocable = globalTaskDispatcher.asyncDispatch(new Runnable() {
            @Override
            public void run() {
                try {
                    connection.connect();
                    InputStream inputStream = connection.getInputStream();
                    byte[] data = read(inputStream);
                    String html = new String(data, "UTF-8");
                    getUITaskDispatcher().syncDispatch(()   -> {
                        text.setText(html);
                    });

                } catch(Exception e) {

                }
            }

            public byte[] read(InputStream inStream) throws Exception{
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while((len = inStream.read(buffer)) != -1)
                {
                    outStream.write(buffer,0,len);
                }
                inStream.close();
                return outStream.toByteArray();
            }
        });

    }
}
